package GameEngine.Model;

import GameEngine.helper.PAppletBridge;

public class CoordinateMapper {

	// Koordinatenmapping
	private int MapX1 = 0;
	private int MapY1 = 0;

	private int MapX2 = 300 * 32;
	private int MapY2 = 25 * 32;

	private float Long1 = 0.0f; // X-Koordinate
	private float Lat1 = 0.0f; // Y-Koordinate

	private float Long2 = 96.0f;
	private float Lat2 = 8.0f;

	// Mapgroesse
	private int MapWidth = MapX2 - MapX1;
	private int MapHeight = MapY2 - MapY1;

	private float WorldWidth = Long2 - Long1;
	private float WorldHeight = Lat2 - Lat1;

	// Konstruktor
	public CoordinateMapper(int mx1, int my1, int mx2, int my2, float lng1,
			float lat1, float lng2, float lat2) {

		setMap1(mx1, my1, lng1, lat1);
		setMap2(mx2, my2, lng2, lat2);
	}

	// Mapping setzen
	public void setMap1(int x1, int y1, float long1, float lat1) {
		MapX1 = x1;
		MapY1 = y1;
		Long1 = long1;
		Lat1 = lat1;
	}

	public void setMap2(int x2, int y2, float long2, float lat2) {
		MapX2 = x2;
		MapY2 = y2;
		Long2 = long2;
		Lat2 = lat2;
	}

	// Mappingfunktionen
	public int longitudeToMapX(float lng) {
		if (WorldWidth != 0) {
			return (int) PAppletBridge.lerp(MapX1, MapX2, (lng / WorldWidth));
		}
		return 0;
	}

	public int latitudeToMapY(float lat) {
		if (WorldHeight != 0) {
			return (int) PAppletBridge.lerp(MapY1, MapY2, (lat / WorldHeight));
		}
		return 0;
	}

	public float mapXToLongitude(int posx) {
		if (MapWidth != 0) {
			float result = PAppletBridge.lerp(Long1, Long2,
					(posx / (MapWidth * 1.0f)));
			return result;
		}

		return (0.0f);
	}

	public float mapYToLatitude(int posy) {
		if (MapHeight != 0) {
			float result = PAppletBridge.lerp(Lat1, Lat2,
					(posy / (MapHeight * 1.0f)));
			return result;
		}

		return (0.0f);
	}
}
